local ziyi = fk.CreateSkill{
  name = "xiaobai__ziyi",
  tags = {
    Skill.Switch
  },
  attached_skill_name = "xiaobai__ziyi&",
}

Fk:loadTranslationTable{
  ["xiaobai__ziyi"] = "恣逸",
  ["xiaobai__ziyi&"] = "恣逸",
  [":xiaobai__ziyi"] = "转换技，阳：你可以视为使用【酒】；阴：其他角色的出牌阶段，其可交给你至少一张牌，"..
    "并令你发动“神点”，然后若你因“神点”摸牌数少于其交出牌数，你失去1点体力。",
  [":xiaobai__ziyi&"] = "你可以交给拥有“恣逸”且处于阴状态的角色一张牌并令其发动”神点“，"..
    "然后若其因此”神点“摸牌数少于你交出牌数，其失去1点体力。"
}

ziyi:addEffect("viewas",{
  anim_type = "switch",
  pattern = "analeptic",
  view_as = function (self, player, cards)
    local wine = Fk:cloneCard("analeptic")
    wine.skillName = ziyi.name
    return wine
  end,
  enabled_at_play = function (self, player)
    return player:getSwitchSkillState(ziyi.name) == fk.SwitchYang
  end,
  enabled_at_response = function (self, player, response)
    return player:getSwitchSkillState(ziyi.name) == fk.SwitchYang and not response
  end
})

local ziyi_other = fk.CreateSkill{
  name = "xiaobai__ziyi&"
}

ziyi_other:addEffect("active",  {
  mute = true,
  can_use = function (self, player)
    local reciver = table.filter(Fk:currentRoom().alive_players, function(p) return p:hasSkill(ziyi.name) 
      and p:getSwitchSkillState(ziyi.name) == fk.SwitchYin end)
    return #reciver > 0
  end,
  card_filter = Util.TrueFunc,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    local reciver = table.filter(Fk:currentRoom().alive_players, function(p) return p:hasSkill(ziyi.name) 
      and p:getSwitchSkillState(ziyi.name) == fk.SwitchYin end)
    return #selected == 0 and table.contains(reciver, to_select)
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return #selected == 1 and #selected_cards > 0
  end,
  on_use = function (self, room, effect)
    local player = effect.tos[1]
    local target = effect.from

    room:notifySkillInvoked(player, ziyi.name, "switch")
    player:broadcastSkillInvoke(ziyi.name)
    
    room:setPlayerMark(
      player,
      MarkEnum.SwithSkillPreName .. ziyi.name,
      player:getSwitchSkillState(ziyi.name, true)
    )
    player:addSkillUseHistory(ziyi.name)
    player:addSkillUseHistory("xiaobai__shendian")
    room:notifySkillInvoked(player, "xiaobai__shendian", "control")
    player:broadcastSkillInvoke("xiaobai__shendian")
    
    room:obtainCard(player, effect.cards, false, fk.ReasonGive, target, ziyi.name)

    local suits = {}
    local discards = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insert(discards, info.cardId)
          end
        end
      end
    end, Player.HistoryTurn)

    for _, discard in ipairs(discards) do
      suits[Fk:getCardById(discard).suit] = (suits[Fk:getCardById(discard).suit] or 0) + 1
    end
    room:handleAddLoseSkills(player, "#xiaobai__shendian")
    local num = math.min(#discards, 20)
    for i = 1, num do
      if #player:getCardIds("he") == 0 then break end
      local cards = room:askToCards(player, {
        max_num = 1,
        min_num = 1,
        skill_name = "xiaobai__shendian",
        include_equip = true,
        cancelable = false,
        prompt = "#xiaobai__shendian:::"..i,
      })
      if #cards > 0 then
        room:recastCard(cards, player, "xiaobai__shendian")
      else
        break
      end
    end
    room:handleAddLoseSkills(player, "-#xiaobai__shendian")
    if player:getMark("xiaobai__shendian_draw") < #effect.cards then
      room:loseHp(player, 1, ziyi.name)
    end
    room:setPlayerMark(player, "xiaobai__shendian_draw", 0)
  end
})

return {ziyi, ziyi_other}